package q1652_decrypt;

public class Solution_1 {
    /*
    通过一个滑动窗口来解决问题，每次滑动的过程中维护窗口的两侧指针
    移除一个数的同时加入下一个数
     */
    public int[] decrypt(int[] code, int k) {
        int[] ans = new int[code.length];
        if (k > 0) {
            int temp = 0;
            for (int i = 1; i <= k; ++i) {
                temp += code[i];
            }
            int l = 1, r = k;
            for (int i = 0; i < ans.length; ++i) {
                ans[i] = temp;
                temp -= code[(l++) % code.length];
                temp += code[(++r) % code.length];
            }
        } else if (k < 0) {
            int temp = 0;
            k = -k;
            for (int i = 0; i < k; ++i) {
                temp += code[code.length - 1 - i];
            }
            int l = code.length - k, r = code.length - 1;
            for (int i = 0; i < ans.length; ++i) {
                ans[i] = temp;
                temp -= code[(l++) % code.length];
                temp += code[(++r) % code.length];
            }
        }
        return ans;
    }
}
